package drops.dao;


import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.util.Criteria;
import org.apache.torque.util.Transaction;

  
  
/**
 * This class was autogenerated by Torque on:
 *
 * [Fri Mar 19 07:17:38 SGT 2010]
 *
 * You should not use this class directly.  It should not even be
 * extended all references should be to Tooth
 */
public abstract class BaseTooth extends BaseObject
{
    /** The Peer class */
    private static final ToothPeer peer =
        new ToothPeer();

        
    /** The value for the toothNumber field */
    private int toothNumber;
      
    /** The value for the patientNumber field */
    private int patientNumber;
      
    /** The value for the value field */
    private int value;
      
    /** The value for the notes field */
    private String notes;
  
    
    /**
     * Get the ToothNumber
     *
     * @return int
     */
    public int getToothNumber()
    {
        return toothNumber;
    }

                        
    /**
     * Set the value of ToothNumber
     *
     * @param v new value
     */
    public void setToothNumber(int v) 
    {
    
                  if (this.toothNumber != v)
              {
            this.toothNumber = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the PatientNumber
     *
     * @return int
     */
    public int getPatientNumber()
    {
        return patientNumber;
    }

                              
    /**
     * Set the value of PatientNumber
     *
     * @param v new value
     */
    public void setPatientNumber(int v) throws TorqueException
    {
    
                  if (this.patientNumber != v)
              {
            this.patientNumber = v;
            setModified(true);
        }
    
                          
                if (aPatient != null && !(aPatient.getPatientNumber() == v))
                {
            aPatient = null;
        }
      
              }
  
    /**
     * Get the Value
     *
     * @return int
     */
    public int getValue()
    {
        return value;
    }

                        
    /**
     * Set the value of Value
     *
     * @param v new value
     */
    public void setValue(int v) 
    {
    
                  if (this.value != v)
              {
            this.value = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the Notes
     *
     * @return String
     */
    public String getNotes()
    {
        return notes;
    }

                        
    /**
     * Set the value of Notes
     *
     * @param v new value
     */
    public void setNotes(String v) 
    {
    
                  if (!ObjectUtils.equals(this.notes, v))
              {
            this.notes = v;
            setModified(true);
        }
    
          
              }
  
      
    
                  
    
        private Patient aPatient;

    /**
     * Declares an association between this object and a Patient object
     *
     * @param v Patient
     * @throws TorqueException
     */
    public void setPatient(Patient v) throws TorqueException
    {
            if (v == null)
        {
                    setPatientNumber(0);
                  }
        else
        {
            setPatientNumber(v.getPatientNumber());
        }
                aPatient = v;
    }

                                            
    /**
     * Get the associated Patient object
     *
     * @return the associated Patient object
     * @throws TorqueException
     */
    public Patient getPatient() throws TorqueException
    {
        if (aPatient == null && (this.patientNumber > 0))
        {
                          aPatient = PatientPeer.retrieveByPK(SimpleKey.keyFor(this.patientNumber));
              
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               Patient obj = PatientPeer.retrieveByPK(this.patientNumber);
               obj.addTooths(this);
            */
        }
        return aPatient;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey.  e.g.
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
           */
    public void setPatientKey(ObjectKey key) throws TorqueException
    {
      
                        setPatientNumber(((NumberKey) key).intValue());
                  }
       
                
    private static List fieldNames = null;

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static synchronized List getFieldNames()
    {
        if (fieldNames == null)
        {
            fieldNames = new ArrayList();
              fieldNames.add("ToothNumber");
              fieldNames.add("PatientNumber");
              fieldNames.add("Value");
              fieldNames.add("Notes");
              fieldNames = Collections.unmodifiableList(fieldNames);
        }
        return fieldNames;
    }

    /**
     * Retrieves a field from the object by name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    public Object getByName(String name)
    {
          if (name.equals("ToothNumber"))
        {
                return new Integer(getToothNumber());
            }
          if (name.equals("PatientNumber"))
        {
                return new Integer(getPatientNumber());
            }
          if (name.equals("Value"))
        {
                return new Integer(getValue());
            }
          if (name.equals("Notes"))
        {
                return getNotes();
            }
          return null;
    }
    
    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    public Object getByPeerName(String name)
    {
          if (name.equals(ToothPeer.TOOTH_NUMBER))
        {
                return new Integer(getToothNumber());
            }
          if (name.equals(ToothPeer.PATIENT_NUMBER))
        {
                return new Integer(getPatientNumber());
            }
          if (name.equals(ToothPeer.VALUE))
        {
                return new Integer(getValue());
            }
          if (name.equals(ToothPeer.NOTES))
        {
                return getNotes();
            }
          return null;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    public Object getByPosition(int pos)
    {
            if (pos == 0)
        {
                return new Integer(getToothNumber());
            }
              if (pos == 1)
        {
                return new Integer(getPatientNumber());
            }
              if (pos == 2)
        {
                return new Integer(getValue());
            }
              if (pos == 3)
        {
                return getNotes();
            }
              return null;
    }
     
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws Exception
     */
    public void save() throws Exception
    {
          save(ToothPeer.getMapBuilder()
                .getDatabaseMap().getName());
      }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
       * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
       *
     * @param dbName
     * @throws TorqueException
     */
    public void save(String dbName) throws TorqueException
    {
        Connection con = null;
          try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
      }

      /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
      /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    public void save(Connection con) throws TorqueException
    {
          if (!alreadyInSave)
        {
            alreadyInSave = true;


  
            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    ToothPeer.doInsert((Tooth) this, con);
                    setNew(false);
                }
                else
                {
                    ToothPeer.doUpdate((Tooth) this, con);
                }
            }

                      alreadyInSave = false;
        }
      }


                                          
  
    private final SimpleKey[] pks = new SimpleKey[2];
    private final ComboKey comboPK = new ComboKey(pks);
    
    /**
     * Set the PrimaryKey with an ObjectKey
     *
     * @param key
     */
    public void setPrimaryKey(ObjectKey key) throws TorqueException
    {
        SimpleKey[] keys = (SimpleKey[]) key.getValue();
        SimpleKey tmpKey = null;
                      setToothNumber(((NumberKey)keys[0]).intValue());
                        setPatientNumber(((NumberKey)keys[1]).intValue());
              }

    /**
     * Set the PrimaryKey using SimpleKeys.
     *
         * @param int toothNumber
         * @param int patientNumber
         */
    public void setPrimaryKey( int toothNumber, int patientNumber)
        throws TorqueException
    {
            setToothNumber(toothNumber);
            setPatientNumber(patientNumber);
        }

    /**
     * Set the PrimaryKey using a String.
     */
    public void setPrimaryKey(String key) throws TorqueException
    {
        setPrimaryKey(new ComboKey(key));
    }
  
    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
              pks[0] = SimpleKey.keyFor(getToothNumber());
                  pks[1] = SimpleKey.keyFor(getPatientNumber());
                  return comboPK;
      }

 

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
       * It then fills all the association collections and sets the
     * related objects to isNew=true.
       */
      public Tooth copy() throws TorqueException
    {
        return copyInto(new Tooth());
    }
  
    protected Tooth copyInto(Tooth copyObj) throws TorqueException
    {
          copyObj.setToothNumber(toothNumber);
          copyObj.setPatientNumber(patientNumber);
          copyObj.setValue(value);
          copyObj.setNotes(notes);
  
                    copyObj.setToothNumber(0);
                              copyObj.setPatientNumber(0);
                        
        
        return copyObj;
    }

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public ToothPeer getPeer()
    {
        return peer;
    }

    public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("Tooth:\n");
        str.append("ToothNumber = ")
           .append(getToothNumber())
           .append("\n");
        str.append("PatientNumber = ")
           .append(getPatientNumber())
           .append("\n");
        str.append("Value = ")
           .append(getValue())
           .append("\n");
        str.append("Notes = ")
           .append(getNotes())
           .append("\n");
        return(str.toString());
    }
}
